package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.AssessmentMapper;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.PsychologyMapper;
import com.itheima.pojo.Member;
import com.itheima.service.PsychologyService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PsychologyService.class)
@Transactional
public class PsychologyServiceImpl implements PsychologyService {
    @Autowired
    private PsychologyMapper psychologyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private AssessmentMapper assessmentMapper;

    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        //mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        //根据条件查询
        Page<Map> page = psychologyMapper.findPage(queryString);
        long total = page.getTotal();
        List<Map> result = page.getResult();
        for (Map map : result) {
            for (Object key : map.keySet()) {
                if (key.equals("regTime")) {
                    Date DateTime = (Date) map.get(key);
                    try {
                        String regTime = DateUtils.parseDate2String(DateTime);
                        map.put("regTime",regTime);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return new PageResult(total,result);
    }

    //获取文件名
    public String findFileName(String fileNumber) {
        //根据用户档案号查询用户id
        //根据用户id查询文件名
        String fileName = psychologyMapper.findFileName(fileNumber);
        return fileName;
    }

    //获取编辑回显数据
    public Map getMember(String fileNumber) {
        Map memberPsy = psychologyMapper.getMemberPsy(fileNumber);
        return memberPsy;
    }

//    编辑会员体质辨识
    public void add(Map map) {
        psychologyMapper.add(map);
    }

    //删除信息
    public Map delete(String fileNumber) {
        //根据会员档案查询会员id
        Member member = memberMapper.getMember(fileNumber);
        Integer memberId = member.getId();
        //根据会员id删除order表
        memberMapper.deleteOrderByMemberId(memberId);

        //查询该会员的体检报告名称
        Map fileNameMap = memberMapper.findFileName(memberId);

        //根据会员id删除体检报告表
        memberMapper.deleteDoc(memberId);

        //删除会员的健康测评记录--体质
        assessmentMapper.deleteMember(memberId);
        //删除会员的健康测评记录--心理
        psychologyMapper.deleteMember(memberId);

        //根据会员id删除会员
        memberMapper.deleteById(memberId);
        return fileNameMap;
    }
}
